
#ifdef WIN32
#include "Windows.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#endif

#include "log_api.h"
#include "OSHeaders.h"
#include "MainThread.h"
#include "StreamServer.h"

//static bool g_bQuit = false;
static MainThread g_MainThread;

static void StartAlprFirmware()
{
    printf("-------Start-------[%s,%d]\n",__FILE__,__LINE__);


    StreamServer server(1);
    g_MainThread.SetStreamServer(&server);

    g_MainThread.Initialize();

    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 1000 * 30;
    server.SetPort(40713);
    server.AddTimerEvent(StreamServer::TimeOutCb,tv,false);
    server.StartRun();
/*
    while(!g_bQuit)
    {
        OSThread::Sleep(1000);
    }
    */
}

static UInt32 u32SignalFlag = 0;
void HandleSig(int signo)
{
    fprintf(stderr,"signo=%d",signo);
    if(u32SignalFlag)
    {
        exit(-1);
    }
#ifndef WIN32
    if(SIGINT == signo || SIGTERM == signo)
    {
        u32SignalFlag++;
        //g_bQuit = true;
        g_MainThread.StopAndWaitForThread();
       // server.StopRun();
        u32SignalFlag--;
    }
#endif
    exit(-1);
}
int main(int argc,char* argv[])
{
#ifdef __Win32__
	WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD(2, 2);
	WSAStartup(wVersionRequested, &wsaData);
#else
    signal(SIGINT,HandleSig);
    //signal(SIGSEGV,HandleSig);
    signal(SIGTERM,HandleSig);
    signal(SIGPIPE,SIG_IGN);
#endif
    log_initialize(".",LOG_OUT_CONSOLE);

    StartAlprFirmware();
#ifdef __Win32__
	WSACleanup();
#endif
	return 0;
}
